package edu.netjava.BiLinkList;

public class BiLinkList {

	BiLinkList linklist;
	Node headNode = null;
	Node currentNode = headNode;
	int index = -1;

	/**
	 * the constructor to initial
	 */
	public BiLinkList() {

	}

	/**
	 * do the add action
	 */
	public void add(String object) {

		Node node = new Node();
		node.setData(object);
		node.setNext(null);
		node.setPrior(null);

		index++;
		/**
		 * set the head node
		 */
		if (headNode == null) {
			headNode = node;
			System.out.println("headNode:" + node.getData());
			currentNode = headNode;
		} else {
			currentNode.setNext(node);// add node
			node.setPrior(currentNode);
			currentNode = node;
		}

	}

	/**
	 * 
	 * @param index
	 *            the location to insert
	 * @param str
	 *            the value of the element to insert
	 */
	public void insertNode(int index, String str) {

		Node nodeFinder = new Node();
		nodeFinder = findNode(index);
		Node newNode = new Node();
		newNode.setData(str);

		nodeFinder.getPrior().setNext(newNode);
		newNode.setPrior(nodeFinder.getPrior());
		newNode.setNext(nodeFinder);
		index++;
	}

	/**
	 * 
	 * @param index :
	 *            the location of the deleted element
	 * @return : the deleted element
	 */
	public Object delete(int index) {
		// deal when the index = 0

		Node node = new Node();
		if (index == 0) {
			headNode = headNode.getNext();
			return headNode.getData();
		}

		// when index is not zero
		Node delNode = headNode;
		for (int i = 0; i < index; i++) {
			delNode = delNode.getNext();
		}
		index--;
		node = delNode.getPrior();
		node.setNext(delNode.getNext());
		delNode.getNext().setPrior(node);
		return delNode.getData();
	}

	/**
	 * 
	 * @param index:location
	 *            of the finding node
	 * @return :the found node
	 */
	public Node findNode(int index) {
		Node node = headNode;
		for (int i = 0; i < index; i++) {
			node = node.getNext();
		}
		return node;
	}

	/**
	 * 
	 * @return the last element
	 */
	public Node getTail() {
		Node node = headNode;
		while (node.getNext() != null) {
			node = node.getNext();
		}
		return node;
	}

	/**
	 * 
	 * @param node :
	 *            a node to return its next
	 * @return : result
	 */
	public boolean hasNext(Node node) {
		return (node.getNext() != null);
	}

	/**
	 * @return
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return linklist.toString();
	}

	/**
	 * print all elements in the list
	 */
	public void printAll() {
		Node node = new Node();
		node = headNode;
		while (node != null) {
			output(node.getData());
			node = node.getNext();
		}
		// output("the data has been given out!");

	}

	/**
	 * print the elements from the tail
	 */
	public void printFromTail() {
		Node node = new Node();
		node = getTail();
		while (node != null) {
			System.out.println(node.getData());
			node = node.getPrior();
		}
	}

	/**
	 * 
	 * @param object:the
	 *            data to print
	 */
	public static void output(Object object) {
		System.out.println(object);
	}

}
